<?php

class Showlist extends CI_Model {
	
	function __construct()	{
      parent::__construct();
   }
	
   public function countRecord($where = '') {
   
   	if(!$where)
   		return $this->db->count_all("members");
   	elseif($where === 'com') {
   		$this->load->database();
   		$this->db->select('*');
   		$this->db->from('members');
   		$where="(`email_id` != '' OR `mobile` != '')";
		$this->db->where($where, NULL, FALSE);
   		return $this->db->count_all_results();
   	}
   	elseif($where === 'nocom') {
   		$this->load->database();
   		$this->db->select('*');
   		$this->db->from('members');
   		$this->db->where('email_id', '');
		$this->db->where('mobile', '');
   		return $this->db->count_all_results();
   	}
   	elseif($where === 'users') {
   		$this->load->database();
   		$this->db->select('*');
   		$this->db->from('users');
   		return $this->db->count_all_results();
   	}
   	
   	 
   }
   
   public function retrieveDept() {
   	$this->load->database();
   	$this->db->select('deptId, deptName');
   	$this->db->from('dept');
   	$query = $this->db->get();
   	//print_r($this->db->last_query());
   	return $query->result();
   }
   
   public function add_member() {
   		$data = array(
   				'name' => $this->input->post('name') ,
   				'batch' => $this->input->post('batch'),
   				'dept' => $this->input->post('dept'),
   				'current_company' => $this->input->post('comp'),
   				'curren_loc' => $this->input->post('loc'),
   				'current_desig' => $this->input->post('design'),
   				'mobile' => $this->input->post('mob'),
   				'email_id' => $this->input->post('mail')
   		);
   		$this->db->insert('members', $data);
   		return $this->db->insert_id();
   }
   
   
  	public function record_count($vbatch = '', $vdept= '', $vsearch = '') {
  		if(!empty($vbatch))
		{
			$this->db->where('batch', $vbatch);
		}
		if(!empty($vdept))
		{
			$this->db->where('dept', $vdept);
		}
		if(!empty($vsearch))
		{
			$where="(`name` like '%$vsearch%' OR `current_company` like '%$vsearch%' OR `curren_loc` like '%$vsearch%')";
			$this->db->where($where, NULL, FALSE);
		}
	   	//print_r($this->db->last_query()); exit;
		return $this->db->count_all_results('members');
	
   }

	public function show_all_alumni() {
		$this->load->database();
		$this->db->select('*');
		$this->db->from('members');
		$this->db->join('dept', 'members.dept = dept.deptId');
		$query = $this->db->get();
		//print_r($this->db->last_query());
		return $query->result();
	} 
	
	public function fetch_alumni($limit = '', $start = '', $vbatch = '', $vdept= '', $vsearch = '') {
		if(isset($vsearch)) {
			$this->session->set_userdata('search', $vsearch); }
		if(isset($vdept)) {
			$this->session->set_userdata('dept', $vdept); }
		if(isset($vbatch)) {
			$this->session->set_userdata('batch', $vbatch); }
		$this->db->limit($limit, $start);
		$this->db->select('*');
		$this->db->from('members');
		$this->db->join('dept', 'members.dept = dept.deptId');
		if(!empty($vbatch))
		{
			$this->db->where('batch', $vbatch);
		}
		if(!empty($vdept))
		{
			$this->db->where('dept', $vdept);
		}
		if(!empty($vsearch))
		{
			$where="(`name` like '%$vsearch%' OR `current_company` like '%$vsearch%' OR `curren_loc` like '%$vsearch%')";
			$this->db->where($where, NULL, FALSE);
		}
		$this->db->order_by("name", "ASC");
		$query = $this->db->get();
		//print_r($this->db->last_query()); 
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row) {
				$data[] = $row;
			}
			return $data;
		}
		return false;
	}
	
}

